<?php

/**
 * controller for the user request
 * 
 * @author Youssef Driouach
 *
 */
class RequestController extends Zend_Controller_Action
{
	public function init()
	{
		
	}
	
	public function indexAction()
	{
		//TODO [move from init()] the code in the IndexAction "le code se duplique dans la boucle"
		$script = "/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
					var disqus_shortname = 'driyou'; // required: replace example with your forum shortname
			
					/* * * DON'T EDIT BELOW THIS LINE * * */
					(function () {
					var s = document.createElement('script'); s.async = true;
					s.type = 'text/javascript';
					s.src = 'http://' + disqus_shortname + '.disqus.com/count.js';
					(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
					}());";
		$this->view->headScript()->appendScript($script, $type = 'text/javascript');
		// the get parameters
		$id_request = $this->_request->getParam("id_request");
		
		$reqMapper = new Application_Model_RequestMapper();
		$request = null;
		$request = $reqMapper->find($id_request, new Application_Model_Request());
		
		$auth = Zend_Auth::getInstance ();
		if ($auth->hasIdentity () && $request != null) {
			
			$userConnected = new Application_Model_User();
			$userMapper = new Application_Model_UserMapper();
			$userConnected = $userMapper->find($auth->getIdentity ()->id_user);
			$objectOwner = $request->getUser();
			
			if($userConnected == $objectOwner )
			{
				try {
					$index = Zend_Search_Lucene::open(APPLICATION_PATH . 'search_index');
					Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength(1);
					Zend_Search_Lucene_Analysis_Analyzer::setDefault(
					new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive()
					);
					$queryString = $request->getKeyword().$request->getOptionss();
					//$queryString = 'golf AND price:[000000900 TO 000010000] AND year:[2001 TO 2009] AND energy:1 AND transmission:2 AND mileage:[175000 TO 200000]';
					$query = Zend_Search_Lucene_Search_QueryParser::parse($queryString,'utf-8');
					    //$query = Zend_Search_Lucene_Search_QueryParser::parse($queryString);
					    //$hits  = $index->find($query);
					//$queryString ="name:{a TO ml} OR description:{a TO lm}";
					//var_dump($queryString);
					$results = $index->find($queryString);
					//var_dump($queryString);
					if($results)
    				{
    					
    					$offers = array();
    					$addresses = array();
    					$class = 'other';
    					foreach($results as $result)
    					{
    						$offer = new Application_Model_Offer();
    						$offerMapper = new Application_Model_OfferMapper();
    						$whereOffer = array('id_offer = ?' => $result->offerId);
    						//build the offer rent or sale
    						$offer = $offerMapper->findWhere($whereOffer);
    						//@TODO check if offer is not null
    						if($offer != null){
	    						$offers[] = $offer;
	    						$address = $offer->getAddresses();
	    						$addresses[] = $address[0];
	    						$class = $offer->getClasse();
    						}
    						//var_dump($result->offerId);
    					}
    					//var_dump($offers);
    					//var_dump($addresses);
    					//@TODO check if offer is not null
    					$offerClass = null;
    					if($offer != null){
    						$offerClass = $offer->getClasse();
    					}
    					$modules = array_merge(array('class' => $class),array('addresses' => $addresses));
    					
    					$this->view->offers = $offers;
    					$this->view->modules = $modules;
    					
    					
    				}
    				else{
    					$this->view->alertType = 'info';
    					$this->view->alert = '0 results';
    				}
				} catch (Zend_Search_Lucene_Search_QueryParserException $e) {
					echo "Query syntax error: " . $e->getMessage() . "\n";
				}
				
			}
		}else{
				
			$this->_helper->redirector ( 'index', 'index' );
		}
	}
	
	public function requestAction() 
	{
		$id_request = $this->_request->getParam("id_request");
		$token = $this->_request->getParam("token");
		$this->view->token = $token;
		$reqMapper = new Application_Model_RequestMapper();
		$request = null;
		$request = $reqMapper->find($id_request, new Application_Model_Request());
		$objectOwner = $request->getUser();
		//check if the user connected is the request owner 
		$supbutton = false;
		$auth = Zend_Auth::getInstance();
		if($auth->hasIdentity()){
			$userMapper = new Application_Model_UserMapper();
			$userConnected = $userMapper->find($auth->getIdentity ()->id_user);
			if($userConnected == $objectOwner)
				$supbutton = true;
		}
		$verb = "";
		$location = "";
		
		try {
				$index = Zend_Search_Lucene::open(APPLICATION_PATH . 'search_index');
				Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength(1);
				Zend_Search_Lucene_Analysis_Analyzer::setDefault(
				new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive()
				);
				$queryString = $request->getKeyword().$request->getOptionss();
				$query = Zend_Search_Lucene_Search_QueryParser::parse($queryString,'utf-8');
				$results = $index->find($queryString);
				
				if($request->getOfferClass() != "")
					$verb = "wants to ".strtolower($request->getOfferClass())." a(n)";
				else 
					$verb = "look for a(n)";
				if($request->getCountry() != "")
					$location = "in";
				
				$this->view->userUrl = '/profil/'.$objectOwner->getUserId();
				$this->view->supbutton = $supbutton;
				$this->view->location = $location;
				$this->view->verb = $verb;
				//$this->classe = strtolower($request->getClass());
				$this->view->objectOwner = $objectOwner;
				$this->view->request = $request;
				$this->view->numberOfResults = count($results);
			} catch (Zend_Search_Lucene_Search_QueryParserException $e) {
				echo "Query syntax error: " . $e->getMessage() . "\n";
			}
	} 
}